package com.c2.web.backend;



import com.c2.entity.EbOrder;
import com.c2.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize",defaultValue = "5") int pageSize) {
        System.out.println("start service....;psize==>"+ psize);
        // 请求service 层获取新闻的分页数据
        List<EbOrder> orderList = orderService.listByPage(psize, pageSize);
        // 调用service层获取新闻表的总页数
        int totalPage = orderService.getMaxPage(pageSize);
        model.addAttribute("orderList",orderList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        return "manage/order";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        //1,参数校验
        //2,执行删除（真实项目中 delete from 表名 where id=?）
        int result = orderService.delete(id);
        if(result == 0) {
            return "manage/manage-error-order";
        }
        return "manage/manage-result-order";
    }
    @RequestMapping("/edit")
    public String edit(Model model,Integer eoId) {
        EbOrder ebOrder = orderService.selectById(eoId);
        model.addAttribute("ebOrder",ebOrder);
        System.out.println(ebOrder);
        return "manage/order-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbOrder ebOrder){
        //1,更新新闻信息
        int result = orderService.updateById(ebOrder);
        //2,跳转到更新结果页面
        if (result<=0){
            return "manage/manage-error-order";
        }
        return "manage/manage-result-order";
    }
    @RequestMapping("/list1")
    public String list1(Model model,  Integer eoId,@RequestParam(value = "eoUserName") String eoUserName , @RequestParam(value = "page",defaultValue = "1") int psize,
                        @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        System.out.println(eoId);
        System.out.println("<-分割线->");
        System.out.println(eoUserName);
        System.out.println(eoUserName=="");
        System.out.println(eoId==null);
        int totalPage=1;
        List<EbOrder> orderList= orderService.selectByIdName2(eoId,eoUserName,psize,pageSize);
        if (eoId==null){
         totalPage = orderService.getByName(eoUserName,pageSize);}
        System.out.println(orderList.size());
        model.addAttribute("eoName",eoUserName);
        model.addAttribute("eoId",eoId);
        model.addAttribute("orderList",orderList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        System.out.println(orderList);
        return "manage/order1";
//        return "redirect:to_longin";重定向
    }
}
